﻿using AntdUI;
using PosClient.Model;
using PosClient.Service;
using PosClient.ViewModel;
using ReactiveUI;
using Splat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.View
{
    public partial class ScreenView : Window, IViewFor<MainViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (MainViewModel)value;
        }

        public MainViewModel ViewModel { get; set; }

        public ScreenView()
        {
            InitializeComponent();
            InitBinding();
            InitEvent();
        }

        private void InitBinding()
        {
            ViewModel = Locator.Current.GetService<MainViewModel>();
            this.WhenActivated(d =>
            {
                // 会员绑定
                this.OneWayBind(ViewModel,
                    vm => vm.VipBase,
                    v => v.lableCode.Text,
                    this.MemberCardConverter
                    ).DisposeWith(d);

                //会员积分
                this.OneWayBind(ViewModel,
                    vm => vm.VipBase.PointCurrent,
                    v => v.lableBonus.Text)
                .DisposeWith(d);
                //会员余额
                this.OneWayBind(ViewModel,
                    vm => vm.VipBase.DepositCurrent,
                    v => v.labelDeposit.Text)
                .DisposeWith(d);

                //会员等级
                this.OneWayBind(ViewModel,
                    vm => vm.VipBase.VipLevel.LevelName,
                    v => v.lableLevel.Text)
                .DisposeWith(d);

                //会员优惠券数量
                this.OneWayBind(ViewModel,
                    vm => vm.VipBase.TicketCount,
                    v => v.lableTicket.Text)
                .DisposeWith(d);

                // 合计总额绑定
                this.OneWayBind(ViewModel,
                    vm => vm.TotalAmount,
                    v => v.label_totalamount.Text,
                    this.TotalAmountConverter
                ).DisposeWith(d);


                // 合计总数绑定
                this.OneWayBind(ViewModel,
                    vm => vm.TotalCount,
                    v => v.label_totalcount.Text,
                    this.TotalAmountConverter
                ).DisposeWith(d);

                // 优惠总额绑定
                this.OneWayBind(ViewModel,
                    vm => vm.TotalDisAmount,
                    v => v.label_totaldiscount.Text,
                    this.TotalAmountConverter
                ).DisposeWith(d);
                // 应付金额绑定
                this.OneWayBind(ViewModel,
                    vm => vm.RemainPayAmount,
                    v => v.label_remainamount.Text,
                    this.RemainPayAmountConverter
                ).DisposeWith(d);


                // 找零金额绑定
                this.OneWayBind(ViewModel,
                    vm => vm.ExchangeAmount,
                    v => v.label_exchangeamount.Text,
                    this.ExchangeAmountConverter
                ).DisposeWith(d);

                this.ViewModel.WhenAnyValue(vm => vm.Index).Subscribe(x =>
                {
                    if (x > -1)
                    {
                        pluGrid1.Index = x;
                    }
                }).DisposeWith(d);

            });
            pluGrid1.DataSource = ViewModel.PluList;
        }

        private string MemberCardConverter(VipBase member)
        {
            return member == null ? "- -" : $"{member.VipName}\r\n{member.VipCode}";
        }

        private string TotalAmountConverter(decimal value)
        {
            return string.Format("{0,8:F2}", value);
        }

        private string RemainPayAmountConverter(decimal value)
        {
            return string.Format("{0,8:F2}", value);
        }

        private string ExchangeAmountConverter(decimal value)
        {
            return string.Format("{0,8:F2}", value);
        }

        public void ShonOnMonitor()
        {
            Screen[] sc;
            sc = Screen.AllScreens;
            int index = sc.Length - 1;
            FormBorderStyle = FormBorderStyle.None;
            StartPosition = FormStartPosition.Manual;
            WindowState = FormWindowState.Maximized;
            if (sc.Length > 1)
            {
                //get all the screen width and heights
                Left = sc[index].Bounds.Width;
                Top = sc[index].Bounds.Height;
                Location = sc[index].Bounds.Location;
                Point p = new Point(sc[index].Bounds.Location.X, sc[index].Bounds.Location.Y);
                Location = p;
                carousel1.Width = (int)(sc[index].Bounds.Width * 0.8);
                Show();
            }
            else
            {
                this.Hide();
            }

        }

        private void InitEvent()
        {
            ViewModel.PluList.ListChanged += (s, e) =>
            {
                pluGrid1.DataSource_ListChanged(s, e);
            };

            this.Load += (s, e) =>
            {
                List<string> imageList = new List<string>();
                imageList.Add("https://gitee.com/cookies_code/boundless/raw/master/doc/image/03.png");
                imageList.Add("https://gitee.com/cookies_code/boundless/raw/master/doc/image/02.png");
                imageList.Add("https://gitee.com/cookies_code/boundless/raw/master/doc/image/01.png");
                foreach (var url in imageList)
                { 
                    var carouselItem = new AntdUI.CarouselItem();
                    carouselItem.Img = ToolService.GetPictureImage(url);
                    this.carousel1.Image.Add(carouselItem);
                }
                carousel1.Autoplay = true;
                carousel1.Autodelay = 5;
            };
        }

    }
}
